using System.Collections;

namespace LightCAD.Three
{
    public class Vector4 : Vector, IIOArray
    {
        #region Properties

        public double x;
        public double y;
        public double z;
        public double w;

        #endregion

        #region constructor
        public Vector4() : this(0, 0, 0, 0)
        {

        }
        public Vector4(double x = 0, double y = 0, double z = 0, double w = 1)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            this.w = w;
        }
        #endregion

        #region properties
        public double width
        {
            get
            {
                return this.z;
            }
            set
            {
                this.z = value;
            }
        }
        public double height
        {
            get
            {
                return this.w;
            }
            set
            {
                this.w = value;
            }
        }
        #endregion

        #region methods
        public Vector4 set(double x, double y, double z, double w)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            this.w = w;
            return this;
        }
        public Vector4 setScalar(double scalar)
        {
            this.x = scalar;
            this.y = scalar;
            this.z = scalar;
            this.w = scalar;
            return this;
        }
        public Vector4 setX(double x)
        {
            this.x = x;
            return this;
        }
        public Vector4 setY(double y)
        {
            this.y = y;
            return this;
        }
        public Vector4 setZ(double z)
        {
            this.z = z;
            return this;
        }
        public Vector4 setW(double w)
        {
            this.w = w;
            return this;
        }
        public Vector4 setComponent(double index, double value)
        {
            switch (index)
            {
                case 0: this.x = value; break;
                case 1: this.y = value; break;
                case 2: this.z = value; break;
                case 3: this.w = value; break;
                default: throw new Error("index is out of range: " + index);
            }
            return this;
        }
        public double getComponent(double index)
        {
            switch (index)
            {
                case 0: return this.x;
                case 1: return this.y;
                case 2: return this.z;
                case 3: return this.w;
                default: throw new Error("index is out of range: " + index);
            }
        }
        public Vector4 clone()
        {
            return new Vector4(this.x, this.y, this.z, this.w);
        }
        public Vector4 copy(Vector4 v)
        {
            this.x = v.x;
            this.y = v.y;
            this.z = v.z;
            this.w = (v.w != double.NaN) ? v.w : 1;
            return this;
        }
        public Vector4 add(Vector4 v)
        {
            this.x += v.x;
            this.y += v.y;
            this.z += v.z;
            this.w += v.w;
            return this;
        }
        public Vector4 addScalar(double s)
        {
            this.x += s;
            this.y += s;
            this.z += s;
            this.w += s;
            return this;
        }
        public Vector4 addVectors(Vector4 a, Vector4 b)
        {
            this.x = a.x + b.x;
            this.y = a.y + b.y;
            this.z = a.z + b.z;
            this.w = a.w + b.w;
            return this;
        }
        public Vector4 addScaledVector(Vector4 v, double s)
        {
            this.x += v.x * s;
            this.y += v.y * s;
            this.z += v.z * s;
            this.w += v.w * s;
            return this;
        }
        public Vector4 sub(Vector4 v)
        {
            this.x -= v.x;
            this.y -= v.y;
            this.z -= v.z;
            this.w -= v.w;
            return this;
        }
        public Vector4 subScalar(double s)
        {
            this.x -= s;
            this.y -= s;
            this.z -= s;
            this.w -= s;
            return this;
        }
        public Vector4 subVectors(Vector4 a, Vector4 b)
        {
            this.x = a.x - b.x;
            this.y = a.y - b.y;
            this.z = a.z - b.z;
            this.w = a.w - b.w;
            return this;
        }
        public Vector4 multiply(Vector4 v)
        {
            this.x *= v.x;
            this.y *= v.y;
            this.z *= v.z;
            this.w *= v.w;
            return this;
        }
        public Vector4 multiplyScalar(double scalar)
        {
            this.x *= scalar;
            this.y *= scalar;
            this.z *= scalar;
            this.w *= scalar;
            return this;
        }
        public Vector4 applyMatrix4(Matrix4 m)
        {
            double x = this.x, y = this.y, z = this.z, w = this.w;
            double[] e = m.elements;
            this.x = e[0] * x + e[4] * y + e[8] * z + e[12] * w;
            this.y = e[1] * x + e[5] * y + e[9] * z + e[13] * w;
            this.z = e[2] * x + e[6] * y + e[10] * z + e[14] * w;
            this.w = e[3] * x + e[7] * y + e[11] * z + e[15] * w;
            return this;
        }
        public Vector4 divideScalar(double scalar)
        {
            return this.multiplyScalar(1 / scalar);
        }
        public Vector4 setAxisAngleFromQuaternion(Quaternion q)
        {
            // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
            // q is assumed to be normalized
            this.w = 2 * JMath.acos(q.w);
            double s = JMath.sqrt(1 - q.w * q.w);
            if (s < 0.0001)
            {
                this.x = 1;
                this.y = 0;
                this.z = 0;
            }
            else
            {
                this.x = q.x / s;
                this.y = q.y / s;
                this.z = q.z / s;
            }
            return this;
        }
        public Vector4 setAxisAngleFromRotationMatrix(Matrix4 m)
        {
            // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
            // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
            double angle, x, y, z; // variables for result
            double epsilon = 0.01,      // margin to allow for rounding errors
                epsilon2 = 0.1;     // margin to distinguish between 0 and 180 degrees
            double[] te = m.elements;
            double m11 = te[0], m12 = te[4], m13 = te[8],
                m21 = te[1], m22 = te[5], m23 = te[9],
                m31 = te[2], m32 = te[6], m33 = te[10];
            if ((JMath.abs(m12 - m21) < epsilon) &&
                 (JMath.abs(m13 - m31) < epsilon) &&
                 (JMath.abs(m23 - m32) < epsilon))
            {
                // singularity found
                // first check for identity matrix which must have +1 for all terms
                // in leading diagonal and zero in other terms
                if ((JMath.abs(m12 + m21) < epsilon2) &&
                     (JMath.abs(m13 + m31) < epsilon2) &&
                     (JMath.abs(m23 + m32) < epsilon2) &&
                     (JMath.abs(m11 + m22 + m33 - 3) < epsilon2))
                {
                    // this singularity is identity matrix so angle = 0
                    this.set(1, 0, 0, 0);
                    return this; // zero angle, arbitrary axis
                }
                // otherwise this singularity is angle = 180
                angle = JMath.PI;
                double xx = (m11 + 1) / 2;
                double yy = (m22 + 1) / 2;
                double zz = (m33 + 1) / 2;
                double xy = (m12 + m21) / 4;
                double xz = (m13 + m31) / 4;
                double yz = (m23 + m32) / 4;
                if ((xx > yy) && (xx > zz))
                {
                    // m11 is the largest diagonal term
                    if (xx < epsilon)
                    {
                        x = 0;
                        y = 0.707106781;
                        z = 0.707106781;
                    }
                    else
                    {
                        x = JMath.sqrt(xx);
                        y = xy / x;
                        z = xz / x;
                    }
                }
                else if (yy > zz)
                {
                    // m22 is the largest diagonal term
                    if (yy < epsilon)
                    {
                        x = 0.707106781;
                        y = 0;
                        z = 0.707106781;
                    }
                    else
                    {
                        y = JMath.sqrt(yy);
                        x = xy / y;
                        z = yz / y;
                    }
                }
                else
                {
                    // m33 is the largest diagonal term so base result on this
                    if (zz < epsilon)
                    {
                        x = 0.707106781;
                        y = 0.707106781;
                        z = 0;
                    }
                    else
                    {
                        z = JMath.sqrt(zz);
                        x = xz / z;
                        y = yz / z;
                    }
                }
                this.set(x, y, z, angle);
                return this; // return 180 deg rotation
            }
            // as we have reached here there are no singularities so we can handle normally
            double s = JMath.sqrt((m32 - m23) * (m32 - m23) +
                                (m13 - m31) * (m13 - m31) +
                                (m21 - m12) * (m21 - m12)); // used to normalize
            if (JMath.abs(s) < 0.001) s = 1;
            // prevent divide by zero, should not happen if matrix is orthogonal and should be
            // caught by singularity test above, but I"ve left it in just in case
            this.x = (m32 - m23) / s;
            this.y = (m13 - m31) / s;
            this.z = (m21 - m12) / s;
            this.w = JMath.acos((m11 + m22 + m33 - 1) / 2);
            return this;
        }
        public Vector4 min(Vector4 v)
        {
            this.x = JMath.min(this.x, v.x);
            this.y = JMath.min(this.y, v.y);
            this.z = JMath.min(this.z, v.z);
            this.w = JMath.min(this.w, v.w);
            return this;
        }
        public Vector4 max(Vector4 v)
        {
            this.x = JMath.max(this.x, v.x);
            this.y = JMath.max(this.y, v.y);
            this.z = JMath.max(this.z, v.z);
            this.w = JMath.max(this.w, v.w);
            return this;
        }
        public Vector4 clamp(Vector4 min, Vector4 max)
        {
            // assumes min < max, componentwise
            this.x = JMath.max(min.x, JMath.min(max.x, this.x));
            this.y = JMath.max(min.y, JMath.min(max.y, this.y));
            this.z = JMath.max(min.z, JMath.min(max.z, this.z));
            this.w = JMath.max(min.w, JMath.min(max.w, this.w));
            return this;
        }
        public Vector4 clampScalar(double minVal, double maxVal)
        {
            this.x = JMath.max(minVal, JMath.min(maxVal, this.x));
            this.y = JMath.max(minVal, JMath.min(maxVal, this.y));
            this.z = JMath.max(minVal, JMath.min(maxVal, this.z));
            this.w = JMath.max(minVal, JMath.min(maxVal, this.w));
            return this;
        }
        public Vector4 clampLength(double min, double max)
        {
            double length = this.length();
            return this.divideScalar(length == 0 ? 1 : length).multiplyScalar(JMath.max(min, JMath.min(max, length)));
        }
        public Vector4 floor()
        {
            this.x = JMath.floor(this.x);
            this.y = JMath.floor(this.y);
            this.z = JMath.floor(this.z);
            this.w = JMath.floor(this.w);
            return this;
        }
        public Vector4 ceil()
        {
            this.x = JMath.ceil(this.x);
            this.y = JMath.ceil(this.y);
            this.z = JMath.ceil(this.z);
            this.w = JMath.ceil(this.w);
            return this;
        }
        public Vector4 round()
        {
            this.x = JMath.round(this.x);
            this.y = JMath.round(this.y);
            this.z = JMath.round(this.z);
            this.w = JMath.round(this.w);
            return this;
        }
        public Vector4 roundToZero()
        {
            this.x = (this.x < 0) ? JMath.ceil(this.x) : JMath.floor(this.x);
            this.y = (this.y < 0) ? JMath.ceil(this.y) : JMath.floor(this.y);
            this.z = (this.z < 0) ? JMath.ceil(this.z) : JMath.floor(this.z);
            this.w = (this.w < 0) ? JMath.ceil(this.w) : JMath.floor(this.w);
            return this;
        }
        public Vector4 negate()
        {
            this.x = -this.x;
            this.y = -this.y;
            this.z = -this.z;
            this.w = -this.w;
            return this;
        }
        public double dot(Vector4 v)
        {
            return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
        }
        public double lengthSq()
        {
            return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
        }
        public double length()
        {
            return JMath.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
        }
        public double manhattanLength()
        {
            return JMath.abs(this.x) + JMath.abs(this.y) + JMath.abs(this.z) + JMath.abs(this.w);
        }
        public Vector4 normalize()
        {
            var len = this.length();
            return this.divideScalar(len == 0 ? 1 : len);
        }
        public Vector4 setLength(double length)
        {
            return this.normalize().multiplyScalar(length);
        }
        public Vector4 lerp(Vector4 v, double alpha)
        {
            this.x += (v.x - this.x) * alpha;
            this.y += (v.y - this.y) * alpha;
            this.z += (v.z - this.z) * alpha;
            this.w += (v.w - this.w) * alpha;
            return this;
        }
        public Vector4 lerpVectors(Vector4 v1, Vector4 v2, double alpha)
        {
            this.x = v1.x + (v2.x - v1.x) * alpha;
            this.y = v1.y + (v2.y - v1.y) * alpha;
            this.z = v1.z + (v2.z - v1.z) * alpha;
            this.w = v1.w + (v2.w - v1.w) * alpha;
            return this;
        }
        public bool equals(Vector4 v)
        {
            return ((v.x == this.x) && (v.y == this.y) && (v.z == this.z) && (v.w == this.w));
        }
        public bool equals(IIOArray obj) => equals(obj as Vector4);
        public Vector4 fromArray(double[] array, int offset = 0)
        {
            this.x = array[offset];
            this.y = array[offset + 1];
            this.z = array[offset + 2];
            this.w = array[offset + 3];
            return this;
        }
        IIOArray IIOArray.fromArray(double[] array, int offset)
        {
            return fromArray(array, offset);
        }
        public double[] toArray(double[] array = null, int offset = 0)
        {
            if (array == null) array = new double[4];
            array[offset] = this.x;
            array[offset + 1] = this.y;
            array[offset + 2] = this.z;
            array[offset + 3] = this.w;
            return array;
        }
        public Vector4 fromBufferAttribute(BufferAttribute attribute, int index)
        {
            if (attribute.arrObj.GetValue(0) is double)
            {
                this.x = attribute.getX(index);
                this.y = attribute.getY(index);
                this.z = attribute.getZ(index);
                this.w = attribute.getW(index);
            }
            else if (attribute.arrObj.GetValue(0) is int)
            {
                this.x = attribute.getIntX(index);
                this.y = attribute.getIntY(index);
                this.z = attribute.getIntZ(index);
                this.w = attribute.getIntW(index);
            }
            else
                console.log("warning unknown data!!!!");

            return this;
        }
        public Vector4 random()
        {
            this.x = JMath.random();
            this.y = JMath.random();
            this.z = JMath.random();
            this.w = JMath.random();
            return this;
        }
        #endregion

        public IEnumerator GetEnumerator()
        {
            yield return this.x;
            yield return this.y;
            yield return this.z;
            yield return this.w;
        }

        public override bool equals(Vector obj)
        {
            return this.equals(obj as Vector4);
        }
    }
}
